package com.li.controller.admin;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @Author: zhiqingLi
 * @Description: excel 模版下载
 * @Date: Created in 11:56 2024/3/14
 **/
@Controller
@RequestMapping("/admin")
public class FileDownloadController {

    @GetMapping("/fileDown")
    public ResponseEntity<byte[]> down1(HttpSession session) throws Exception {
        String realPath = session.getServletContext().getRealPath("/static/小区管理.xlsx");
        //创建输入流
        InputStream is = new FileInputStream(realPath);
        //创建字节数组  .available()是获取当前对象的可用长度
        byte[] bytes = new byte[is.available()];
        //将流读到字节数组中
        is.read(bytes);
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes,HttpStatus.OK);
        is.close();
        return responseEntity;
    }
}
